# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : finger.py
# Date      : 2018/12/2
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------


__author__ = "lin lingwei"


import maya.cmds as mc

import configs.constants.attribute_constant as ac
import configs.constants.nodeType_constant as nc
import configs.constants.suffix_constant as sc
import configs.constants.body_constant as bc
import configs.constants.global_constant as gc

import scripts.commonCmds as con
import scripts.curveCmds as crc

import rig.body.bCore as bCore



class Finger(bCore.BodyCore):
    
    def __init__(self, localMake, localPrefix,localTag):
        super(Finger, self).__init__(localTag=localTag,
                                  localMake=localMake,
                                  localPrefix=localPrefix)
        
        # self.localSeq = [gThigh, gLeg, gShank, gAnkle, gToe, gToeTip]
        
    def create(self,tmpData,tag,mirror=False):
        # print(tmpData)
        self.localInit()

        localSeq = tmpData["seq"]
        
        jotlist = self.localJoint(tmpData, localSeq=localSeq, mirror=mirror)
    
        tmpjotlist = [tmpData[_] for _ in localSeq]
    
        if self.localPrefix == gc.gLeft:
            localColorId = bc.cLeft
        else:
            localColorId = bc.cRight
    
        fkObj = ft.FkTool()
        fkCtrlist, fkCtrljotlist = fkObj.controlChain("_".join([self.localName,tag]),
                                                      jotlist[:-1], tmpjotlist[:-1],
                                                      parent=True, mirror=mirror, ctrlColorId=localColorId)

        for ctr,cjot, fjot in zip(fkCtrlist,fkCtrljotlist, jotlist[:-1]):
            mc.parent(mc.parentConstraint(cjot, fjot, mo=True),self.localStatic)
            
            sc = con.objToType(cjot,name=con.lname(fjot,bc.aLocalScale),mode=1)
            mc.parent(sc,fkObj.localGrp)
            mc.scaleConstraint(cjot, sc, mo=True)
            mc.connectAttr(sc+".s",fjot+".s",f=True)
            
            mc.addAttr(ctr, ln=bc.aLocalScale, at='float', dv=1, min=0.01, k=True)

            mc.connectAttr(ctr+"."+bc.aLocalScale,cjot+".sx")
            mc.connectAttr(ctr+"."+bc.aLocalScale,cjot+".sy")
            mc.connectAttr(ctr+"."+bc.aLocalScale,cjot+".sz")
        if mc.objExists(con.lname(self.localPrefix,bc.gWrist)):
            mc.parent(jotlist[0],con.lname(self.localPrefix,bc.gWrist))
            
        mc.parent(fkObj.localGrp, self.localGrp)
        
        attr = "%sCurl"%tag
        mc.addAttr(self.localBridge,ln=attr,at="float",dv=0,max=10,min=-2,k=True)
        
        for ctr in fkCtrlist[1:]:
            cp = mc.listRelatives(ctr,p=True)[0]
            #setDrivenKeyframe -currentDriver L_Finger_Fk_Thumb4_ctrl.translateX L_Finger_Fk_Thumb4_ctrl.translateZ;

            mc.setDrivenKeyframe(cp+".rz",cd=self.localBridge+"."+attr,dv=0,v=0,ott="linear",itt="linear")
            mc.setDrivenKeyframe(cp+".rz",cd=self.localBridge+"."+attr,dv=10,v=-90,ott="linear",itt="linear")
            mc.setDrivenKeyframe(cp+".rz",cd=self.localBridge+"."+attr,dv=-10,v=90,ott="linear",itt="linear")

        tp = con.objToLocator(jotlist[0], con.lname(self.localName, bc.gFk,tag, "toParent"),
                                hide=True, parent=False)
        mc.parent(fkObj.follow["point"], fkObj.follow["orient"],fkObj.follow["scale"], tp)
        self.follow[bc.gParent]=tp
        
        mc.sets(fkCtrljotlist,include = self.localSkinJotSet)
        return
    
        
        
if __name__ == '__main__':
    pass